package com.mhc.suzuki.service.impl;

import com.mhc.suzuki.api.model.DataDictDTO;
import com.mhc.suzuki.api.service.IDataDictService;
import com.mhc.suzuki.constant.ErrorCodeEnum;
import com.mhc.suzuki.constant.GroupTypeEnum;
import com.mhc.suzuki.constant.IsDeletedEnum;
import com.mhc.suzuki.dal.mapper.ext.DataDictExtMapper;
import com.mhc.suzuki.dal.model.DataDictDO;
import com.mhc.suzuki.dal.query.DataDictQuery;
import com.mhc.suzuki.exception.SuzukiCommonException;
import com.mhc.suzuki.manager.DataDictManager;
import com.subaru.common.util.BeanCopierUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HW
 * on 2017/8/17 9:51.
 */
@Slf4j
@RestController
@Transactional(rollbackFor = Exception.class)
public class DataDictServiceImpl implements IDataDictService {

    @Autowired
    protected DataDictExtMapper dataDictExtMapper;

    @Autowired
    protected DataDictManager dataDictManager;


    /**
     * 获取元数据分组列表
     * @param companyNo 公司编号
     * @return
     */
    @Override
    public List<DataDictDTO> getDataGroupList(String companyNo) throws SuzukiCommonException{

        /*查询分组列表*/
        //条件设置
        DataDictQuery dataDictQuery = new DataDictQuery();
        DataDictQuery.Criteria criteria = dataDictQuery.createCriteria();
        if(StringUtils.isBlank(companyNo)) {
            log.info("companyNo is null, unable to query");
            throw new SuzukiCommonException(ErrorCodeEnum.DATA_ERROR);
        }
        criteria.andDataGroupEqualTo(GroupTypeEnum.GROUPLIST.getStatus())
                .andIsDeletedNotEqualTo(IsDeletedEnum.DELETED.getCode())
                .andCompanyNoEqualTo(companyNo);
        List<DataDictDO> dataDictDOS = dataDictManager.selectByQuery(dataDictQuery);
        List<DataDictDTO> dataDictDTOS = new ArrayList<>();
        for(DataDictDO dataDictDO : dataDictDOS){
            DataDictDTO dataDictDTO = BeanCopierUtil.convert(dataDictDO, DataDictDTO.class);
            dataDictDTOS.add(dataDictDTO);
        }
        return dataDictDTOS;
    }

    /**
     * 获取dataGroup分组下的元数据列表
     * @param dataGroupCode 分组code
     * @param companyNo 公司编号
     * @return
     */
    @Override
    public List<DataDictDTO> getGroupDataList(String dataGroupCode, String companyNo) throws SuzukiCommonException{
        //验证该分组是否存在
        int size = getCodeGroupByGroupCodeAndCompanyId(dataGroupCode, companyNo);
        if(size == 1){
            DataDictQuery dataDictQuery = new DataDictQuery();
            dataDictQuery.createCriteria()
                    .andDataGroupEqualTo(dataGroupCode)
                    .andIsDeletedNotEqualTo(IsDeletedEnum.DELETED.getCode())
                    .andCompanyNoEqualTo(companyNo);
            List<DataDictDO> dataDictDOS = dataDictManager.selectByQuery(dataDictQuery);
            List<DataDictDTO> dataDictDTOS = new ArrayList<>();
            for(DataDictDO dataDictDO : dataDictDOS){
                DataDictDTO dataDictDTO = BeanCopierUtil.convert(dataDictDO, DataDictDTO.class);
                dataDictDTOS.add(dataDictDTO);
            }
            return dataDictDTOS;
        } else {
            throw new SuzukiCommonException(ErrorCodeEnum.DATA_ERROR);
        }
    }


    /**
     * 添加元数据
     * @param dataDictDTO 元数据具体数据
     * @return MsgCodeEnum.getCode
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addDataDict(DataDictDTO dataDictDTO) throws SuzukiCommonException {
        DataDictDO dataDictDO = BeanCopierUtil.convert(dataDictDTO, DataDictDO.class);

        DataDictQuery dataDictQuery = new DataDictQuery();
        /*查找同company,dataGroup,code的datadict*/
        if (StringUtils.isBlank(dataDictDO.getCode())){
            throw new SuzukiCommonException(ErrorCodeEnum.PARAM_ERROR);
        }
        dataDictQuery.createCriteria()
                .andCodeEqualTo(dataDictDO.getCode())
                .andIsDeletedNotEqualTo(IsDeletedEnum.DELETED.getCode())
                .andCompanyNoEqualTo(dataDictDO.getCompanyNo())
                .andDataGroupEqualTo(dataDictDO.getDataGroup());

        if (dataDictManager.selectByQuery(dataDictQuery).size() == 0) {
            long result = dataDictManager.insertSelective(dataDictDO);
            if (result != 1L){
                log.info(String.valueOf(result));
                throw new SuzukiCommonException(ErrorCodeEnum.INSERT_OR_UPDATA_FAIL);
            }
        } else {
            throw new SuzukiCommonException(ErrorCodeEnum.DATA_ERROR);
        }
    }



    /**
     * 修改元数据信息
     * @param dataDictDTO 修改后的数据
     * @Param dataGroupCode 分组code
     * @Param dataGroupCode 分组code
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDict(DataDictDTO dataDictDTO) throws SuzukiCommonException{
        DataDictDO dataDictDO = BeanCopierUtil.convert(dataDictDTO, DataDictDO.class);
        DataDictQuery dataDictQuery = new DataDictQuery();
        dataDictQuery.createCriteria().andDataIdEqualTo(dataDictDO.getDataId());
        if(dataDictExtMapper.updateByQuerySelective(dataDictDO, dataDictQuery) != 1){
            throw new SuzukiCommonException(ErrorCodeEnum.INSERT_OR_UPDATA_FAIL);
        }
    }


    /**
     * 删除元数据
     * @param dataDictDTO 元数据ID
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDataDict(DataDictDTO dataDictDTO) throws SuzukiCommonException{
        DataDictDO dataDictDO = BeanCopierUtil.convert(dataDictDTO, DataDictDO.class);
        if(dataDictExtMapper.updateByPrimaryKeySelective(dataDictDO) != 1){
            throw new SuzukiCommonException(ErrorCodeEnum.INSERT_OR_UPDATA_FAIL);
        }
    }


    /**
     * 根据companyid, groupcode确定是否存在该分组
     * @param groupCode
     * @param companyNo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int getCodeGroupByGroupCodeAndCompanyId(String groupCode, String companyNo) throws SuzukiCommonException{
        DataDictQuery groupQuery = new DataDictQuery();
        DataDictQuery.Criteria groupCriteria = groupQuery.createCriteria();
        groupCriteria.andIsDeletedNotEqualTo(IsDeletedEnum.DELETED.getCode())
                .andCompanyNoEqualTo(companyNo)
                .andCodeEqualTo(groupCode);
        return dataDictManager.selectByQuery(groupQuery).size();
    }



}
